package com.rrqs.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rrqs.activity.common.Common;
import com.rrqs.activity.data.PaihChildData;
import com.rrqs.activity.data.PaihData;
import com.rrqs.frame.JsonProcessHelper;
import com.rrqs.views.PullDownView;
import com.rrqs.views.PullDownView.OnPullDownListener;
import com.rrqs.views.ScrollOverListView;
import com.zadtek.xmqishi.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 排行榜child View fragment
 *
 * @author kuyukeji
 */
public class MyCheckChildFragment extends Fragment {

    private Context context;
    private LayoutInflater inflater;
    private Activity myActivity;
    private Resources res;

    private PullDownView pullDownView;
    private ScrollOverListView listView;

    private LinearLayout nolay;        //空数据

    public List<PaihChildData> orderLists; // 订单的列表
    public PaihData paihData;
    private ArrayList<PaihChildData> initList = null; // 点击和刷新的列表

    public ThreadAdapter threadAdapter;

    private SharedPreferences sp;
    private String state = "1"; // 订单状态( 1.单量 2.准时率 3.平均时长 4.配送里程)
    private String time = "1"; // 1.今天 2.昨天 3.本月

//    private TextView psthreeview;    //第3个表格的标题


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    // 设置订单的状态
    public void setState(String state) {
        this.state = state;
    }

    //设置选择的月份
    public void setTime(String time) {
        this.time = time;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_paih_child, null);
        context = view.getContext();
        this.inflater = inflater;
        myActivity = getActivity();
        res = getResources();

        pullDownView = (PullDownView) view.findViewById(R.id.pulldownview);
        pullDownView.enableAutoFetchMore(true, 0);
        listView = pullDownView.getListView();
        listView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        listView.setDividerHeight(0);



        //没有订单
        nolay = (LinearLayout) view.findViewById(R.id.nolay);

        threadAdapter = new ThreadAdapter();
        listView.setAdapter(threadAdapter);

        getinitArray();

        pullDownView.setOnPullDownListener(new OnPullDownListener() {
            @Override
            public void onRefresh() {
                // TODO Auto-generated method stub
                initArrays(new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        if (!"emp".equals(msg.obj.toString())) {
                            paihData = (PaihData) msg.obj;
                            if ("0".equals(paihData.getStatus())) {
                                orderLists = paihData.getOther_danliang();
                                PaihChildData childData = new PaihChildData();
                                childData.setOther_danliang_all(paihData.getMe_danliang_all());
                                childData.setOther_dianzan(paihData.getMe_dianzan());
                                childData.setOther_image(paihData.getMe_image());
                                childData.setOther_name(paihData.getMe_name());
                                childData.setIndex(paihData.getMe_danliang());
                                if (orderLists == null ){
                                    orderLists = new ArrayList<>();
                                }
                                orderLists.add(0,childData);
                            }
                            threadAdapter.notifyDataSetChanged();
                            if (orderLists.size() > 0) {
                                if (paihData.getStatus().equals("0")) {
                                    nolay.setVisibility(View.GONE);
                                    pullDownView.setVisibility(View.VISIBLE);
                                } else {
                                    Common.DisplayToast(context, paihData.getMessage(), 1);
                                    // 没有订单
                                    nolay.setVisibility(View.VISIBLE);
                                    pullDownView.setVisibility(View.GONE);
                                }
                            }
                        } else {
                            Common.DisplayToast(context, "通信失败,请检查网络!", 1);
                        }
                        pullDownView.notifyDidRefresh(true);
                        pullDownView.notifyDidLoadMore(true);
                    }
                });
            }

            @Override
            public void onLoadMore() {
                // TODO Auto-generated method stub
            }
        });

        return view;
    }

    // 获取初始化数据
    public void getinitArray() {
        initArrays(new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (!"emp".equals(msg.obj.toString())) {
                    paihData = (PaihData) msg.obj;
                    if ("0".equals(paihData.getStatus())) {
                        orderLists = paihData.getOther_danliang();
                        PaihChildData childData = new PaihChildData();
                        childData.setOther_danliang_all(paihData.getMe_danliang_all());
                        childData.setOther_dianzan(paihData.getMe_dianzan());
                        childData.setOther_image(paihData.getMe_image());
                        childData.setOther_name(paihData.getMe_name());
                        childData.setIndex(paihData.getMe_danliang());
                        if (orderLists == null){
                            orderLists = new ArrayList<>();
                        }
                        orderLists.add(0,childData);
                    }
                    threadAdapter.notifyDataSetChanged();
                    pullDownView.notifyDidDataLoad(false);

                    if (orderLists.size() > 0) {
                        if (paihData.getStatus().equals("0")) {
                            nolay.setVisibility(View.GONE);
                            pullDownView.setVisibility(View.VISIBLE);
                        } else {
                            Common.DisplayToast(context, paihData.getMessage(), 1);
                            // 没有订单
                            nolay.setVisibility(View.VISIBLE);
                            pullDownView.setVisibility(View.GONE);
                        }
                    }
                    pullDownView.notifyDidLoadMore(true);

                } else {
                    nolay.setVisibility(View.GONE);
                    pullDownView.setVisibility(View.GONE);

                    pullDownView.notifyDidDataLoad(false);
                    if (initList == null) {
                        Common.DisplayToast(context, "通信失败,请检查网络!", 1);
                    } else {
                        pullDownView.notifyDidLoadMore(true);
                    }
                }
            }
        });
    }

    // 线程加载数据
    private void initArrays(final Handler handler) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {


                    paihData = JsonProcessHelper.jsonProcess_getqstotalph("get_getorder_tanking", time, state);
                    if (paihData != null) {
                        handler.obtainMessage(0, paihData).sendToTarget();
                    } else {
                        handler.obtainMessage(0, "emp").sendToTarget();
                    }

                } catch (Exception e) {
                    Thread.interrupted();
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void refresh() {
        getinitArray();
    }


    public class ThreadAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return orderLists == null ? 0 : orderLists.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView,ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.fragment_paih_childitem, null);

                holder.other_image = (ImageView) convertView.findViewById(R.id.other_image);
                holder.index = (TextView) convertView.findViewById(R.id.index);
                holder.other_name = (TextView) convertView.findViewById(R.id.other_name);
                holder.other_danliang_all = (TextView) convertView.findViewById(R.id.other_danliang_all);
                holder.other_dianzan = (TextView) convertView.findViewById(R.id.other_dianzan);
                holder.line = convertView.findViewById(R.id.line);
                holder.line2 = convertView.findViewById(R.id.line2);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            PaihChildData data = orderLists.get(position);
            if (position == 0){
                holder.line.setVisibility(View.VISIBLE);
                holder.line2.setVisibility(View.GONE);
                holder.index.setText(data.getIndex() + "");
            } else {
                holder.line.setVisibility(View.GONE);
                holder.line2.setVisibility(View.VISIBLE);
                holder.index.setText("" + (position));
            }

            Glide.with(context).load(data.getOther_image()).placeholder(R.drawable.user_icon).into(holder.other_image);

            holder.other_name.setText(data.getOther_name());
            holder.other_danliang_all.setText(data.getOther_danliang_all()+ "");
            holder.other_dianzan.setText(data.getOther_dianzan()+ "");

            return convertView;
        }

    }

    static class ViewHolder {
        private View line;
        private View line2;
        private TextView index; // 排名
        private ImageView other_image;    //奖杯图标
        private TextView other_name; // 配送人
        private TextView other_danliang_all; // 配送区域
        private TextView other_dianzan; // 配送评分
    }

}
